<template>
  <div class="log">
      <div class="MainTop_left">
          <div @click="toMain" v-bind='$attrs'>首页</div>
          <div :class="['beyond_list iconlist iconfont', beyondSH]" v-if="topBeyond.length>0" >
              <div class="beyond_list_div">
                  <span :style="[url == v.url ? { background: '#fff' } : '']" @click="hisUrl(v)" v-for="(v, i) in topBeyond" :key="i">
                      <i :class="['iconfont', v.icon]"></i>
                      {{ v.title }}
                  </span>
              </div>
          </div>
          <div
            class="close_item"
            v-for="(item, index) in topList"
            :style="[(nowUrl?nowUrl:url) == item.url ? { background: '#16A98C' } : '']"
            :key="index">
              <span @click="hisUrl(item)">
                  <i :class="['iconfont', item.icon]"></i>
                  {{ item.title }}
                  <span class="close iconchahao iconfont" @click.stop="close(index)"></span>
              </span>
              <i class="zhankai el-icon-s-operation"></i>
              
              <div class="close_list">
                  <span @click="closeLeft(index)">
                      <span class="iconfont iconzuojiantou-"></span> 关闭左侧
                  </span>
                  <span @click="closeRight(index)">
                      <span class="iconfont iconyoujiantou-"></span> 关闭右侧
                  </span>
                  <span @click="closeAll">
                      <span class="iconfont iconmoduanzuoyouzhankai"></span> 关闭全部
                  </span>
              </div>
          </div>
      </div>

      <div class="MainTop_right">
          <div class="changeTheme" v-if="isChecked">
            <!-- <svg
              t="1597632748987"
              class="icon"
              viewBox="0 0 1024 1024"
              version="1.1"
              xmlns="http://www.w3.org/2000/svg"
              p-id="2937"
              width="32"
              height="32"
              data-spm-anchor-id="a313x.7781069.0.i6"
            >
              <path
                d="M772.8 96v64L936 321.6l-91.2 91.2c-12.8-11.2-27.2-16-43.2-16-36.8 0-65.6 28.8-65.6 65.6V800c0 35.2-28.8 64-64 64H352c-35.2 0-64-28.8-64-64V462.4c0-36.8-28.8-65.6-65.6-65.6-16 0-32 6.4-43.2 16L88 321.6 249.6 160h40l1.6 1.6C336 228.8 420.8 272 512 272c91.2 0 176-41.6 220.8-110.4 0-1.6 1.6-1.6 1.6-1.6h38.4V96m-481.6 0H256c-22.4 0-38.4 6.4-49.6 19.2L43.2 276.8c-25.6 25.6-25.6 65.6 0 89.6l94.4 94.4c11.2 11.2 27.2 17.6 41.6 17.6s30.4-6.4 41.6-17.6h1.6c1.6 0 1.6 0 1.6 1.6V800c0 70.4 57.6 128 128 128h320c70.4 0 128-57.6 128-128V462.4c0-1.6 0-1.6 1.6-1.6h1.6c11.2 11.2 27.2 17.6 41.6 17.6 16 0 30.4-6.4 41.6-17.6l94.4-94.4c25.6-25.6 25.6-65.6 0-89.6L819.2 115.2C806.4 102.4 790.4 96 772.8 96h-40c-22.4 0-41.6 11.2-54.4 30.4-33.6 49.6-96 81.6-168 81.6s-134.4-33.6-168-81.6C332.8 107.2 312 96 291.2 96z"
                fill="#e6e6e6"
                p-id="2938"
              ></path>
            </svg> -->
            <!-- <svg
              t="1597720200177"
              class="icon"
              viewBox="0 0 1024 1024"
              version="1.1"
              xmlns="http://www.w3.org/2000/svg"
              p-id="2903"
              width="32"
              height="32"
            >
              <path
                d="M772.8 96v64L936 321.6l-91.2 91.2c-12.8-11.2-27.2-16-43.2-16-36.8 0-65.6 28.8-65.6 65.6V800c0 35.2-28.8 64-64 64H352c-35.2 0-64-28.8-64-64V462.4c0-36.8-28.8-65.6-65.6-65.6-16 0-32 6.4-43.2 16L88 321.6 249.6 160h40l1.6 1.6C336 228.8 420.8 272 512 272c91.2 0 176-41.6 220.8-110.4 0-1.6 1.6-1.6 1.6-1.6h38.4V96m-481.6 0H256c-22.4 0-38.4 6.4-49.6 19.2L43.2 276.8c-25.6 25.6-25.6 65.6 0 89.6l94.4 94.4c11.2 11.2 27.2 17.6 41.6 17.6s30.4-6.4 41.6-17.6h1.6c1.6 0 1.6 0 1.6 1.6V800c0 70.4 57.6 128 128 128h320c70.4 0 128-57.6 128-128V462.4c0-1.6 0-1.6 1.6-1.6h1.6c11.2 11.2 27.2 17.6 41.6 17.6 16 0 30.4-6.4 41.6-17.6l94.4-94.4c25.6-25.6 25.6-65.6 0-89.6L819.2 115.2C806.4 102.4 790.4 96 772.8 96h-40c-22.4 0-41.6 11.2-54.4 30.4-33.6 49.6-96 81.6-168 81.6s-134.4-33.6-168-81.6C332.8 107.2 312 96 291.2 96z"
                p-id="2904"
                fill="#d81e06"
              ></path>
            </svg> -->
            <!-- <div class="Switch" v-if="isChecked">
              <div>
                深色
              </div>
              <div>
                亮色
              </div>
            </div> -->
          </div>
          <div class="iconfont">
              &#xe624;
              <div class="Switch">
                  <div @click="changeUser(v.type)" v-for="(v, i) in userdata.callCheck||[]" :key="i" v-show="v.type != 4">
                      {{ v.title }}
                  </div>
                  <div @click="logout()">
                      {{ hasUser }}
                  </div>
              </div>
          </div>
          <span class="welcome" v-if="!isChecked">欢迎您：
              <span>{{ userdata.name ? userdata.name : user }}</span>
          </span>
          <span class="welcome" v-if="isChecked">欢迎您：
               <!-- @click="EditUserNameDialog = true" -->
              <span>{{userdata.name ? userdata.name : user}}</span>
          </span>

          <div class="checkBox">
              <input type="checkbox" v-model="isChecked" @click="ismenuOpen" id="checkBox"/>
              <label for="checkBox"></label>
          </div>
      </div>
      <!-- 修改用户名对话框 -->
      <el-dialog title="修改昵称" :visible.sync="EditUserNameDialog" width="30%" center>
          <el-form ref="form" :model="form" label-width="80px" @submit.native.prevent>
              <el-form-item label="昵称">
                  <el-input v-model="form.userName"></el-input>
              </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
              <el-button @click="EditUserNameDialog = false">取 消</el-button>
              <el-button type="primary" @click="EditUserNameDialog = false">确 定</el-button>
          </span>
      </el-dialog>
  </div>
</template>
<script>
import getKeyToken from "../../public/API/getKeyToken";
import config from "../../public/config";
import {get} from "../../public/API/query";
export default {
    inheritAttrs:false,
    name: "MainTop",
    props:['nowUrl'],
    data() {
      return {
          hasUser: "退出登录",
          userdata: {},
          user: "",
          isChecked: "",
          topList: [],
          topBeyond: [],
          shouList: "hid",
          beyond_listsh: "hid",
          beyondSH: "sho",
          name: "",
          EditUserNameDialog: false,
          form: {
              userName: "",
          },
          isCanSubBug: false,
          url:''
      };
    },
    watch: {
    },
    created() {
        this.userdata = config.get("userInfo");
        this.isChecked = config.get("sys").isMenuOpen;
        var his = config.get("topHis");
        var beyond = config.get("beyond");
        if(window.sessionStorage.getItem('abxnowhis') && his.length==0){
            config.setTopHis(JSON.parse(window.sessionStorage.getItem('abxnowhis')))
            this.url = JSON.parse(window.sessionStorage.getItem('abxnowhis')).url
        }
        this.topList = his;
        this.topBeyond = beyond;
    },
    methods: {
        //返回主页
        toMain() {
            this.$router.push("/");
        },
        //关闭左侧
        closeLeft(index) {
            // console.log(event.target.parentElement)
            var showIndex = config.get("topHis").findIndex(item=>{
                return item.url = this.nowUrl
            })
            if (showIndex<index) {
                this.$router.push(config.get("topHis")[index].url)
            }
            config.get("topHis").splice(0, index);
            if (config.get("beyond").length > index) {
                for (let i = 0; i < index; i++) {
                    config.get("topHis").push(config.get("beyond")[i]);
                }
                config.get("beyond").splice(0, index);
            } else {
                for (let i = 0; i < config.get("beyond").length; i++) {
                    config.get("topHis").push(config.get("beyond")[i]);
                }
                config.get("beyond").splice(0, config.get("beyond").length);
            }
        },
        //关闭右侧
        closeRight(index) {
          // console.log(this.nowUrl)
          // var showIndex = config.get("topHis").findIndex(item=>{
          //   return item.url == this.nowUrl
          // })
          // console.log(showIndex)
          // if (showIndex==0) {
          //   }
            this.$router.push(config.get("topHis")[index].url)
            config.get("topHis").splice(index+1, config.get("topHis").length);
            config.get("beyond").splice(0, config.get("beyond").length);
        },
        //关闭全部
        closeAll() {
            // config.get("topHis").splice(0, config.get("topHis").length);
            // config.get("beyond").splice(0, config.get("beyond").length);
            config.cleHis()
            window.sessionStorage.clear('abxnowhis')
            this.$router.push('/')
        },
        //导航
        hisUrl(item) {
            this.$router.push(item.url);
            window.sessionStorage.setItem('abxnowhis',JSON.stringify(item))
        },
        //关闭点击的
        close(index) {
            if (index==0 && this.nowUrl == this.topList[0].url && this.topList.length>1) {
                this.$router.push(this.topList[1].url)
            }
            // if (this.topList.length==1) {
            //     console.log('2')
                
            // }
            if (index==this.topList.length-1 && this.nowUrl == this.topList[index].url && this.topList.length>1) {
                this.$router.push(this.topList[index-1].url)
            }
            if (config.get("beyond").length > 0) {
                this.topList.splice(index, 1);
                this.topList.push(config.get("beyond")[0]);
                this.topBeyond.splice(0, 1);
            } else {
                if (this.topList.length==1) {
                    this.topList = []
                    this.$router.push('/')
                    config.cleHis()
                    window.sessionStorage.clear('abxnowhis')
                }else{
                    this.topList.splice(index, 1);
                }
            }
        },
        changeUser(type) {
            getKeyToken.keyGetKey(this.userdata.key,type);
        },
        //退出登录
        logout() {
            if (this.hasUser == "退出登录") {
               for (let name of Object.keys(window.localStorage.valueOf())) {
                  if (name != "$adminAccountPassword") {
                    window.localStorage.removeItem(name);
                  }
                }
                window.sessionStorage.clear();
                config.clear();
                this.$router.push("/login");
                // }
                // })
            } else {
                this.$router.push("/login");
            }
        },
        ismenuOpen() {
            this.$emit("opOrCl", this.isChecked);
        },
    },
    watch:{
    },
    mounted() {
        window.addEventListener('beforeunload',()=>{
            if (this.topList.length == 0) {
                window.sessionStorage.removeItem('abxnowhis')
            }
        })
        if (this.userdata) {
            this.hasUser = "退出登录";
        } else {
            this.hasUser = "登录";
        }
        
        if (JSON.parse(window.localStorage.getItem('r-ad-userinfo'))) {
            this.user = config.get("userInfo").username;
            // this.user = this.user.slice(0, 3) + "****" + this.user.slice(7, this.user.length);
           
        }
    },
};
</script>
<style lang="less" scope>
.hid {
    display: none;
}
.sho {
    display: block;
}
.checkBox {
    width: 40px;
    height: 20px;
    margin-top: 20px;
    position: absolute;
    border-radius: 10px;
    right: 87%;
    label {
        width: 100%;
        height: 100%;
        background: #eee;
        position: absolute;
        cursor: pointer;
        top: 0;
        left: 0;
        border-radius: 10px;
        &::before {
            content: "";
            position: absolute;
            left: 0;
            top: 0;
            width: 20px;
            height: 20px;
            border-radius: 10px;
            background: #bcbcbc;
            transition: 0.2s;
        }
    }
}
input[type="checkbox"] {
    position: absolute;
    height: 20px;
    width: 20px;
    left: 0;
    display: none;
    &:checked ~ label::before {
        left: 20px;
    }
    &:checked ~ label {
        background: lightgreen;
    }
}
.log {
    height: 60px;
    background: #18bc9c;
    color: #fff;
    .MainTop_left {
        float: left;
        width: 70%;
        height: 100%;
        position: relative;
        cursor: default;
      .iconfont {
          margin: 0 !important;
      }
      .beyond_list {
          float: left;
          height: 60px;
          width: 13%;
          text-align: center;
          position: absolute;
          right: 0px;
          background: #18bc9c;
          line-height: 60px;
          &:hover {
              background: #16a98c;
              .beyond_list_div {
                  display: block;
              }
          }
          > .beyond_list_div {
                position: absolute;
                top: 60px;
                left: 0;
                width: 100%;
                line-height: 30px;
                color: #000;
                display: none;
                border: 1px solid rgb(228, 222, 222);
                z-index: 101;
                cursor: pointer;
                font-size: 0;
              > span {
                  cursor: pointer;
                  width: 100%;
                  height: 30px;
                  background: rgb(228, 222, 222);
                  white-space: nowrap;
                  overflow: hidden;
                  text-overflow: ellipsis;
                  vertical-align: bottom;
                  &:hover {
                      background: #fff;
                  }
              }
          }
      }
      > div:nth-child(1) {
          float: left;
          padding: 0 20px 0 20px;
          line-height: 60px;
          cursor: pointer;
          &:hover {
              background: #16a98c;
          }
      }
      .close_item {
          float: left;
          cursor: pointer;
          font-size: 15px;
          height: 60px;
          width: calc(~"(87% - 68px)/7");
          text-align: center;
          position: relative;
          user-select: none;
          // box-shadow: -1px 0 1px .5px #ccc inset;
          border-left: 1px solid rgba(56, 55, 55, 0.2);
          &:last-of-type {
              border-right: 1px solid rgba(56, 55, 55, 0.2);
          }
          &:hover{
              .zhankai{
                  display: inline-block;
              }
          }
          
          > span {
              cursor: pointer;
              overflow: hidden;
              position: relative;
              width: 100%;
              line-height: 60px;
              white-space: nowrap;
              overflow: hidden;
              text-overflow: ellipsis;
              .close {
                  visibility: hidden;
                  cursor: pointer;
                  position: absolute;
                  line-height: normal;
                  right: 0;
                  top: 0;
                  z-index: 110;
                  &:hover {
                      background: gray;
                  }
              }
          }
          &:hover {
              background: #16a98c;
              .close {
                  visibility: visible;
              }
          }
          .close_list {
              width: 100%;
              max-height: 0;
              overflow: hidden;
              position: absolute;
              transition: 0.5s;
              color: #000;
              text-align: center;
              z-index: 101;
              left: 0;
              top: 60px;
              border: 1px solid rgb(228, 222, 222);
              font-size: 0;
              &:hover{
                  max-height: 200px;
              }
              >span {
                  width: 100%;
                  height: 30px;
                  line-height: 30px;
                  overflow: hidden;
                  background: rgb(228, 222, 222);
                  &:hover {
                      background: rgb(252, 249, 249);
                  }
              }
          }
          >.zhankai{
              display: none;
              position: absolute;
              bottom: 0;
              right: 0;
              text-align: right;
              line-height: normal;
              &:hover{
                  background: gray;
                  ~.close_list{
                      max-height: 200px;
                  }
              }
          }
      }
    }
    .MainTop_right {
        position: relative;
        width: 30%;
        height: 100%;
        float: right;
        > span {
            font-size: 30px;
            font-family: "Franklin Gothic Medium", "Arial Narrow", Arial, sans-serif;
            background-size: auto 50px;
            font-weight: bold;
            cursor: default;
        }
        .iconfont {
            width: 100px;
            position: absolute;
            right: 10%;
        }
        .welcome {
            position: absolute;
            right: 40%;
            font-size: 18px;
            line-height: 60px;
            > span {
                font-size: 18px;
                color: #6e2de5;
                cursor: pointer;
            }
        }
        > div {
            float: right;
            text-align: center;
            line-height: 60px;
            font-size: 30px;
            transition: 0.5x;
            margin-right: 20px;
            cursor: pointer;
            &:hover .Switch {
                max-height: 800px;
            }
            .Switch {
                background: rgb(84, 92, 100);
                opacity: 1;
                width: 80px;
                position: absolute;
                top: 50px;
                color: #fff;
                max-height: 0;
                overflow: hidden;
                transition: 0.4s;
                border-radius: 5px;
                left: calc(~"50% - 60px");
                z-index: 99;
                margin-left: 15px;
                > div {
                    text-align: center;
                    line-height: 40px;
                    min-height: 40px;
                    width: 100%;
                    &:hover {
                        background: #303133;
                    }
                }
            }
        }
    }
}
.changeTheme {
    position: absolute;
    right: 0;
    margin-top: 6px;
}
.isShowpifu {
    width: 50px;
    height: 50px;
    position: fixed;
    text-align: center;
    bottom: 50px;
    right: 50px;
    background: red;
    color: #fff;
    z-index: 102;
    line-height: 50px;
    border-radius: 50%;
    opacity: 0.5;
    cursor: pointer;
}
</style>
